<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\OrderModel;
use cmf\controller\AdminBaseController;
use think\Db;

class OrderController extends AdminBaseController
{

    /**
     * 幻灯片列表
     *
     * @adminMenu(
     *     'name'   => '幻灯片管理',
     *     'parent' => 'admin/Setting/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 40,
     *     'icon'   => '',
     *     'remark' => '幻灯片管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $OrderModel = new OrderModel();
        $request = input('request.');
        $where = array();
        if (!empty($request['keyword'])) {
            $type = $request['type'];
            $keyword = trim($request['keyword']);
            if ($type == "id") {
                $where['erxi_order.order_id'] = [
                    'like',
                    "%$keyword%",
                ];
            }
            if ($type == "name") {
                $where['erxi_user.user_nickname'] = [
                    'like',
                    "%$keyword%",
                ];
            }
        }
        // dump($where);die;
        $list = $OrderModel
            ->join("erxi_user", "erxi_order.uid=erxi_user.id")
            ->join("erxi_device", "erxi_order.device_id=erxi_device.id")
            ->field("erxi_order.*,erxi_user.user_nickname,erxi_device.device_name")
            ->where($where)->paginate(10);
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function edit()
    {
        $data = $this->request->param();
        $id = $data['id'];
        $model = new DeviceModel();
        $device = $model->find($id);
        $this->assign('device', $device);
        return $this->fetch();
    }

    public function editdevice()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $model = new DeviceModel();
            $model->where([
                'id' => $data['post']['id'],
            ])->update($data['post']);
            $this->success('修改成功!', url('admin/device/index'));
        }
    }

    public function deleteDevice()
    {
        $id = $data = $this->request->param("id");
        $model = new DeviceModel();
        $model->where([
            'id' => $id,
        ])->delete();
        $this->success('删除成功!', url('admin/device/index'));
    }

    public function adddevice()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $model = new DeviceModel();
            $id = $model->insert($data['post']);
            // $post = $data['post'];
            // $result = $this->validate($post, 'AdminArticle');
            // if ($result !== true) {
            // $this->error($result);
            // }

            // $portalPostModel = new PortalPostModel();
            //
            // if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
            // $data['post']['more']['photos'] = [];
            // foreach ($data['photo_urls'] as $key => $url) {
            // $photoUrl = cmf_asset_relative_url($url);
            // array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
            // }
            // }
            //
            // if (!empty($data['file_names']) && !empty($data['file_urls'])) {
            // $data['post']['more']['files'] = [];
            // foreach ($data['file_urls'] as $key => $url) {
            // $fileUrl = cmf_asset_relative_url($url);
            // array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
            // }
            // }
            //
            // $portalPostModel->adminAddArticle($data['post'], $data['post']['categories']);
            //
            // $data['post']['id'] = $portalPostModel->id;
            // $hookParam = [
            // 'is_add' => true,
            // 'article' => $data['post']
            // ];
            // hook('portal_admin_after_save_article', $hookParam);

            $this->success('添加成功!', url('admin/device/index'));
        }
    }

    // 生成订单
    public function generatorOrder($item_id, $uid, $total_fee, $rand)
    {
        $time = date("YmdHis");
        $order_id = $time . $rand;
        $data['order_id'] = $order_id;
        $data['create_time'] = time();
        $data['item_id'] = $item_id;
        $data['status'] = 0;
        $data['uid'] = $uid;
        $data['money'] = $total_fee;
        Db::table('erxi_order')->insert($data);
        return $order_id;
    }
}
